package dd.prog2.exercises.set01;

public class Board {

	private int[][] board = new int[6][7];
	private int player = 1;
	private int turnCounter = 0;
	private boolean gameOver = false;
	private int winner = 0;

	public Board() {
	}

	void drawBoard() {
		System.out.println(" 1  2  3  4  5  6  7\n" + "--------------------");
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 7; j++)
				System.out.print(" " + board[i][j] + " ");
			System.out.println();
		}
		System.out.println("--------------------");
	}

	private void isGameOver() {
		// turnCounter=42? => draw
		if (turnCounter == 42) {
			winner = 0;
			gameOver = true;
		}
		// check winner horiz
		if (!gameOver) {
			for (int i = 0; i < 6; i++) {
				for (int j = 0; j < 3; j++) {
					if (board[i][j] != 0)
						if (board[i][j] == board[i][j + 1]
								&& board[i][j] == board[i][j + 2]
								&& board[i][j] == board[i][j + 3]) {
							// System.out.println("winner found. horizontal. at\n");
							// +board[i][j]+" "+board[i][j+1]+" "+board[i][j+2]+" "+board[i][j+3]);
							winner = board[i][j];
							gameOver = true;
						}
				}
			}
		}
		// check winner vertical
		if (!gameOver) {
			for (int i = 0; i < 3; i++) {
				for (int j = 0; j < 7; j++) {
					if (board[i][j] != 0)
						if (board[i][j] == board[i + 1][j]
								&& board[i][j] == board[i + 2][j]
								&& board[i][j] == board[i + 3][j]) {
							// System.out.println("winner found vertical. at\n");
							// +board[i][j]+" "+board[i+1][j]+" "+board[i+2][j]+" "+board[i+3][j]);
							winner = board[i][j];
							gameOver = true;
						}
				}
			}
		}
		// check winner diag right
		if (!gameOver) {
			for (int i = 0; i < 3; i++) {
				for (int j = 0; j < 4; j++) {
					if (board[i][j] != 0)
						if (board[i][j] == board[i + 1][j + 1]
								&& board[i][j] == board[i + 2][j + 2]
								&& board[i][j] == board[i + 3][j + 3]) {
							// System.out.println("winner found diag.right. at\n");
							// +board[i][j]+" "+board[i+1][j+1]+" "+board[i+2][j+2]+" "+board[i+3][j+3]);
							winner = board[i][j];
							gameOver = true;
						}
				}
			}
		}
		// check winner diag left
		if (!gameOver) {
			for (int i = 0; i < 3; i++) {
				for (int j = 3; j < 7; j++) {
					if (board[i][j] != 0) {
						if (board[i][j] == board[i + 1][j - 1]
								&& board[i][j] == board[i + 2][j - 2]
								&& board[i][j] == board[i + 3][j - 3]) {
							// System.out.println("winner found diag.left. at\n");
							// +
							// board[i][j]+" "+board[i-1][j+1]+" "+board[i-2][j+2]+" "+board[i-3][j+3]);
							winner = board[i][j];
							gameOver = true;
						}

					}
				}
			}
		}
	}

	// setter
	public String setTurn(int column) {
		for (int i = 5; i >= 0; i--) {
			// System.out.println(i+" "+column);
			if (board[i][column] == 0) {
				board[i][column] = player;
				turnCounter++;
				if (player == 1)
					player = 2;
				else
					player = 1;
				if (turnCounter > 4)
					isGameOver();
				break;
				// return "Success";
			}
			if (i == 0 && board[i][column] != 0)
				return "\nUngueltig!!! SPALTE VOLL!!!\n";
		}
		return "";
	}

	// getter
	public int getPlayer() {
		return player;
	}

	public int getWinner() {
		return winner;
	}

	public boolean getGameOver() {
		return gameOver;
	}
}
